package com.gp.controller.alb;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.common.utils.CookieUtils;
import com.gp.controller.BaseController;
import com.gp.model.dto.AlbApplyVo;
import com.gp.model.dto.AuditApplyVo;
import com.gp.model.entity.Areas;
import com.gp.model.entity.BaseAibExchangeLogs;
import com.gp.model.entity.BaseAlbApply;
import com.gp.model.entity.BaseSystemSettings;
import com.gp.model.entity.BaseUser;
import com.gp.service.IBaseAibExchangeLogsService;
import com.gp.service.IBaseAlbApplyService;

/**
 * @date: 2018年1月9日
 * @author: 弓成龙
 * @email: gcx909109@sina.cn
 * @description: 艾乐币控制器
 **/
@RestController
@RequestMapping("/alb")
public class AlbController extends BaseController{
  
  @Autowired
  private IBaseAlbApplyService albService;
  @Autowired
  private IBaseAibExchangeLogsService logService;
  /**
   * 查询用户的所有的艾乐币交易记录
   * @param userId 用户id
   * @return JsonResult
   */
  @RequestMapping("/listTradeRecord")
  public JsonResult listUserTradeRecord(Page page, String userId) {
    page = albService.listUserTradeRecord(page,userId);
    return new JsonResult("检索成功",page);
  }
  
  /**
   * 删除一条兑换记录
   * @param id
   * @return
   */
  @RequestMapping("/delTradeRecord/{id}")
  public JsonResult del(@PathVariable(value="id") String id) {
    boolean flag = logService.deleteById(Long.parseLong(id));
    return flag ? renderSuccess("删除成功") : renderError("删除失败");
  }
  
  /**
   * 交易记录
   * @param page
   * @return
   */
  @RequestMapping("/listRecord")
  public JsonResult list(Page page,@RequestParam(value="isSearch",required=false) Integer isSearch) {
    if(isSearch!=null && isSearch==1) {
      Wrapper<BaseAibExchangeLogs> wapper = new EntityWrapper<>();
      Map<String, Object> condition = page.getCondition();
      wapper.like("nickName", (String)condition.get("nickName"));
      page.setCondition(null);
      Page<BaseAibExchangeLogs> page2 = logService.selectPage(page,wapper);
      page2.setCondition(condition);
      return new JsonResult("检索成功",page2); 
    }else {
       Page<BaseAibExchangeLogs> result = logService.selectPage(page);
    }
    return new JsonResult("检索成功",page);
  }
  
  /**
   * 提交申请
   */
  @RequestMapping("/submitApply")
  public JsonResult submitApply(HttpServletRequest request,AlbApplyVo submitApply) {
    BaseUser user = (BaseUser)CookieUtils.obtainCurrentUser(request);
    Long uid = user.getId(); 
    String username=user.getNickName(); 
   // if ("exchange".equals(submitApply.getType())) {
    if (0==submitApply.getType()) {
      // 处理兑换的申请
      albService.addSubmitApply(submitApply,uid,username);
    // } else if ("withdraw".equals(submitApply.getType())) {
    } else if (1==submitApply.getType()) {
      // 处理体现的申请
      albService.submitWithdrawApply(submitApply,uid,username);
    }else{
      return new JsonResult(500,"提交失败!");
    }
    return new JsonResult(200,"提交成功，等待管理员审核!");
  }
  
  /**
   * 审核申请
   */
  @RequestMapping("/auditApply")
  public JsonResult auditApply(AuditApplyVo auditApply) {
    albService.auditApply(auditApply);
    return renderSuccess("审核完成。");
  }
  
  /**
   * 查询所有等待审核的申请,提供给管理员进行审核
   * @param userId 用户id
   * @return JsonResult
   */
  @RequestMapping("/listWaitAuditApply")
  public JsonResult listWaitAuditApply(Page page) {
    Page result = albService.listWaitAuditApply(page);
    return new JsonResult("检索成功",result);
  }
  
  
  /**
   * 查询申请 
   * @param  
   * @return JsonResult
   */
  @RequestMapping("/list")
  public JsonResult listApply(Page<BaseAlbApply> page, @RequestParam(value = "isSearch", required = false) Integer isSearch) {
    if(isSearch!=null && isSearch==1) {
      Wrapper<BaseAlbApply> wapper = new EntityWrapper<>();
      Map<String, Object> condition = page.getCondition();
      wapper.like("username", (String)condition.get("username"));
      page.setCondition(null);
      Page<BaseAlbApply> page2 = albService.selectPage(page,wapper);
      page2.setCondition(condition);
      return new JsonResult("检索成功",page2); 
    }else {
       Page<BaseAlbApply> result = albService.selectPage(page);
    }
    return new JsonResult("检索成功",page);
  }
}
